﻿Imports System.Data.OleDb
Public Class frmSanPham

    Private Sub frmSanPham_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            LoaiSanPham_Load()
            SanPham_Load()
            'Phân quyền Form
            Dim aPermission(4) As Boolean
            aPermission = PhanQuyenForm("MenuSanPham", frmMain.UserId)
            cmdAddNew.Enabled = aPermission(0)
            cmdEdit.Enabled = aPermission(1)
            cmdDelete.Enabled = aPermission(2)
            cmdPrint.Enabled = aPermission(3)
        Catch ex As Exception
            ShowError(ex)
        End Try
    End Sub
    Public Sub SanPham_Load()
        Try
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dsSanPham As New DataSet
            Dim dtSanPham As New DataTable
            Dim query As String '= "Select * From (Select SanPham.DVTSanPhamID,IDSanPham,MaLoai,TenLoai,MaSanPham,TenSanPham,TenDonViTinh,SanPham.GhiChu From SanPham " & _
            '  " Left Outer Join LoaiSanPham On SanPham.IdLoai=LoaiSanPham.IdLoai" & _
            ' " Left Outer Join DVTSanPham On SanPham.DVTSanPhamId=DVTSanPham.DVTSanPhamId" & _
            '" Where SanPham.Xoa=0 And MaSanPham Like N'%" & txtTenSanPhamSearch.Text & "%' Or TenSanPham Like N'%" & txtTenSanPhamSearch.Text & "%') AS SP Where SP.DVTSanPhamID IS NOT NULL Order By TenSanPham"

            If cbxLoaiSanPham.SelectedValue = 0 Then
                query = "Select * From (Select SanPham.IDLoai,SanPham.DVTSanPhamID,IDSanPham,MaLoai,TenLoai,MaSanPham,TenSanPham,TenDonViTinh,SanPham.GhiChu From SanPham " & _
                                   " Left Outer Join LoaiSanPham On SanPham.IdLoai=LoaiSanPham.IdLoai" & _
                                   " Left Outer Join DVTSanPham On SanPham.DVTSanPhamId=DVTSanPham.DVTSanPhamId" & _
                                   " Where SanPham.Xoa=0 And (MaSanPham Like N'%" & txtTenSanPhamSearch.Text & "%' Or TenSanPham Like N'%" & txtTenSanPhamSearch.Text & "%')) AS SP Where SP.DVTSanPhamID IS NOT NULL Order By IDLoai,TenSanPham"
            Else
                query = "Select * From (Select SanPham.IDLoai,SanPham.DVTSanPhamID,IDSanPham,MaLoai,TenLoai,MaSanPham,TenSanPham,TenDonViTinh,SanPham.GhiChu From SanPham " & _
                                   " Left Outer Join LoaiSanPham On SanPham.IdLoai=LoaiSanPham.IdLoai" & _
                                   " Left Outer Join DVTSanPham On SanPham.DVTSanPhamId=DVTSanPham.DVTSanPhamId" & _
                                   " Where SanPham.Xoa=0 And (MaSanPham Like N'%" & txtTenSanPhamSearch.Text & "%' Or TenSanPham Like N'%" & txtTenSanPhamSearch.Text & "%') ) AS SP Where SP.DVTSanPhamID IS NOT NULL And IDLoai=" & cbxLoaiSanPham.SelectedValue & " Order By IDLoai,TenSanPham"
            End If
            Dim adapter As OleDb.OleDbDataAdapter
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dsSanPham)
            dbConn.Close()
            dgvSanPham.AutoGenerateColumns = False
            dtSanPham = dsSanPham.Tables(0)
            dtSanPham.Columns.Add("STT")
            For i As Integer = 0 To dtSanPham.Rows.Count - 1
                dtSanPham.Rows(i)("STT") = i + 1
            Next
            dgvSanPham.DataSource = dtSanPham
        Catch ex As Exception
            ShowError(ex)
        End Try
    End Sub
    Public Sub LoaiSanPham_Load()
        Try
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dsLoaiSanPham As New DataSet
            Dim dtLoaiSanPham As New DataTable
            Dim query As String = "Select IDLoai,TenLoai From LoaiSanPham Union Select IdTatCa As IDLoai,TatCa As TenLoai From TatCa"
            Dim adapter As OleDb.OleDbDataAdapter
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dsLoaiSanPham)
            dbConn.Close()
            dtLoaiSanPham = dsLoaiSanPham.Tables(0)
            cbxLoaiSanPham.DataSource = dtLoaiSanPham
            cbxLoaiSanPham.ValueMember = "IdLoai"
            cbxLoaiSanPham.DisplayMember = "TenLoai"
        Catch ex As Exception
            ShowError(ex)
        End Try
    End Sub
    Private Sub cmdThemLoai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        ShowModal(frmLoaiSanPham, False, False)
    End Sub
    Public Sub ShowChildForm(ByVal Frm As Form, Optional ByVal MinimumBox As Boolean = True, Optional ByVal MaximumBox As Boolean = True)
        Dim ChildForm As New Form
        For Each ChildForm In Form.ActiveForm.MdiChildren()
            If ChildForm.Name = Frm.Name Then
                ChildForm.Activate()
                Exit Sub
            End If
        Next
        Frm.MdiParent = frmMain.ActiveForm
        Frm.MaximizeBox = MaximumBox
        Frm.MinimizeBox = MinimumBox
        CenterForm(Frm)
        Frm.Show()
    End Sub
    Public Sub ShowModal(ByVal Frm As Form, Optional ByVal MinimumBox As Boolean = True, Optional ByVal MaximumBox As Boolean = True)
        Frm.MaximizeBox = MaximumBox
        Frm.MinimizeBox = MinimumBox
        CenterForm(Frm)
        Frm.ShowDialog()
    End Sub
    Public Sub CenterForm(ByVal Frm As Form)
        Frm.StartPosition = FormStartPosition.CenterScreen
    End Sub
    Private Sub cmdThemSanPham_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'If txtTenSanPham.Text = "" Then
        '    lblEmpty.Visible = True
        '    Exit Sub
        'Else
        '    lblEmpty.Visible = False
        'End If
        'If txtDonGia.Text = "" Then
        '    lblDonGiaEmty.Visible = True
        '    Exit Sub
        'Else
        '    lblDonGiaEmty.Visible = False
        'End If
        'Dim dataAccess As New DataAccess
        'Dim dbConn As New OleDb.OleDbConnection
        'Dim query As String
        'Dim dbCommand As OleDbCommand
        'dataAccess.ConnectDatabase_DBAccess(dbConn)
        ''kiem tra ten san pham do ton tai chua
        'query = String.Format("Select count(*) from SanPham Where TenSanPham='{0}' And IDLoai={1}", txtTenSanPham.Text.Trim(), cbxLoaiSanPham.SelectedValue)
        'dbCommand = New OleDbCommand(query, dbConn)
        'Dim Result As Integer = dbCommand.ExecuteScalar()
        'If Result > 0 Then
        '    MessageBox.Show( _
        '      String.Format("Tên sản phẩm {0} này đã có", txtTenSanPham.Text.Trim()), _
        '              "Thông báo", _
        '        MessageBoxButtons.OK, _
        '              MessageBoxIcon.Question)
        'Else
        '    query = "Insert Into SanPham(IDLoai,TenSanPham,DonGia) Values('" & cbxLoaiSanPham.SelectedValue & "','" & txtTenSanPham.Text & "','" & txtDonGia.Text & "')"
        '    dbCommand = New OleDbCommand(query, dbConn)
        '    dbCommand.ExecuteNonQuery()
        'End If
        'dbConn.Close()
        'SanPham_Load()
    End Sub

    Private Sub txtDonGia_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) Then
            e.Handled = True
        End If
    End Sub

    Private Sub txtDonGia_Leave(ByVal sender As Object, ByVal e As System.EventArgs)
        'If txtDonGia.Text <> "" And NotCurencyFormat(txtDonGia.Text) Then
        '    txtDonGia.Text = CDec(txtDonGia.Text).ToString("N", Globalization.CultureInfo.CreateSpecificCulture("vi-vn"))
        'End If
    End Sub
    'Private Sub cbxLoaiSanPham_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs)
    '    'SanPham_Load()
    '    'lblDonGiaEmty.Visible = False
    '    'lblEmpty.Visible = False
    'End Sub
    Private Sub txtTenSanPhamSearch_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtTenSanPhamSearch.TextChanged
        Try
            SanPham_Load()
        Catch ex As Exception

        End Try
    End Sub
    Private Sub cbxLoaiSanPham_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbxLoaiSanPham.SelectionChangeCommitted
        Try
            SanPham_Load()
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
    Function NotCurencyFormat(ByVal Curency As String) As Boolean
        Dim dotPos As Integer = Curency.IndexOf(".")
        Dim commaPos As Integer = Curency.IndexOf(",")
        If dotPos < 0 Then Return True
        If dotPos >= commaPos Then Return True
        Return False
    End Function

    Private Sub ChinhSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChinhSua.Click
        Try
            
        Catch ex As Exception

        End Try
    End Sub

    Private Sub MenuTimKiem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try

        Catch ex As Exception

        End Try
    End Sub

    Private Sub cmdAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddNew.Click
        Try
            frmThemSanPham.IDSanPham = -1
            ShowModalForm(frmThemSanPham, False, False)
            frmThemSanPham.IDSanPham = -1
            SanPham_Load()
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub cmdEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEdit.Click
        Try
            frmThemSanPham.IDSanPham = Integer.Parse(dgvSanPham.Rows(dgvSanPham.CurrentCell.RowIndex).Cells("IDSanPham").Value.ToString)
            ShowModal(frmThemSanPham, False, False)
            frmThemSanPham.IDSanPham = -1
            SanPham_Load()
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
        Try
            Dim IDSanPham As Integer = Integer.Parse(dgvSanPham.Rows(dgvSanPham.CurrentCell.RowIndex).Cells("IDSanPham").Value.ToString)
            Dim TenSanPham As String = dgvSanPham.Rows(dgvSanPham.CurrentCell.RowIndex).Cells("TenSanPham").Value.ToString
            'Lấy thông tin sản phầm
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dtSanPham As New DataTable
            Dim query As String = "Select SoLuongTon,SoLuongKhoXuLy From SanPham Where IDSanPham=" & IDSanPham
            Dim adapter As OleDb.OleDbDataAdapter
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dtSanPham)
            Dim dlgRes As DialogResult
            dlgRes = MessageBox.Show( _
                  "Sản phẩm : " & TenSanPham & vbCrLf & _
                  "Số lượng tồn trong kho thành phẩm : " & Format(dtSanPham.Rows(0)("SoLuongTon"), "N2") & vbCrLf & _
                  "Số lượng tồn trong kho xử lý : " & Format(dtSanPham.Rows(0)("SoLuongKhoXuLy"), "N2") & vbCrLf & _
                  "Bạn thực sự muốn xóa sản phầm này", _
                  "Xóa loại sản phẩm", _
            MessageBoxButtons.OKCancel, _
                  MessageBoxIcon.Question)
            If dlgRes = DialogResult.OK Then
                'Update
                Dim dbCommand As OleDbCommand
                query = "Update SanPham Set Xoa=1 Where IDSanPham=" & IDSanPham
                dbCommand = New OleDbCommand(query, dbConn)
                dbCommand.ExecuteNonQuery()
                txtTenSanPhamSearch.Text = ""
                SanPham_Load()
            End If

            dbConn.Close()
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub cmdPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrint.Click
        Try

        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub txtTextSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            SanPham_Load()
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
End Class